import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { loginPasswordApi } from '@/types/user'

export const useUserStore = defineStore(
  'user',
  () => {
    const testNum = ref(0)
    const setTestNum = () => {
      testNum.value++
    }
    // 存储用户信息
    const userInfo = ref<loginPasswordApi>()
    const setUserInfo = (info: loginPasswordApi) => {
      userInfo.value = info
    }
    // 退出时删除用户信息与token
    const logout = () => {
      userInfo.value = undefined
    }

    // 回跳地址
    const backUrl = ref('')
    const setBackUrl = (str: string) => {
      backUrl.value = str
    }

    return { testNum, setTestNum, userInfo, setUserInfo, logout, backUrl, setBackUrl }
  },
  {
    // persist: true  // 所有数据持久化
    persist: {
      paths: ['testNum', 'userInfo', 'backUrl']
    }
  }
)
